package com.qf.controller;

import com.qf.pojo.R;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

//@RestController
//@RequestMapping("/user")
public class UserController3 {

    @Autowired
    UserService userService;

    @Autowired
    private RestTemplate restTemplate;

//    //服务发现客户端
//    @Autowired
//    private DiscoveryClient discoveryClient;
//
//    @Autowired //该对象内部封装 DiscoveryClient
//    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/{id}")
    public R findOrders(@PathVariable("id") Integer id) throws InterruptedException {
/*
        ServiceInstance orderInstance = loadBalancerClient.choose("QF-ORDER");

        String ip = orderInstance.getHost();
        int port = orderInstance.getPort();


        //利用restTemplate发送http请求，访问order服务的数据
        R r = restTemplate.getForObject("http://"+ip+":"+port+"/order/" + id, R.class);*/

        R r = restTemplate.getForObject("http://QF-ORDER/order/" + id, R.class);

        return r;
    }
}
